package com.tencent.wxcloudrun.frontcontroller;

import com.tencent.wxcloudrun.dto.*;
import com.tencent.wxcloudrun.mapper.SkulistMapper;
import com.tencent.wxcloudrun.service.GoodsService;
import com.tencent.wxcloudrun.service.GoodsadditionService;
import com.tencent.wxcloudrun.service.PicService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/4 19:25
 * @description: TODO
 */

@RestController
@Api(tags = "商品模块")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PicService picService;

    @Autowired
    private GoodsadditionService goodsadditionService;


    @Autowired
    private SkulistMapper skulistMapper;

    @Operation(summary = "获取商品分类")
    @PostMapping("/wxcloud/goodsCategory")
    GoodsCategoryResponse goodsCategory(HttpServletRequest request, @RequestBody GoodsCategoryRequest json) {

        List<Goodscategory>  goodsCategories =goodsService.getGoodsCategory();
        GoodsCategoryResponse goodsCategoryResponse = new GoodsCategoryResponse();
        goodsCategoryResponse.setCode(0);
        goodsCategoryResponse.setData(goodsCategories);
        return goodsCategoryResponse;
    }



    @Operation(summary = "获取商品分类详情")
    @PostMapping("/wxcloud/goodsCategoryDetail")
    GoodsCategoryDetailResponse goodsCategoryDetail(HttpServletRequest request, @RequestBody GoodsCategoryDetailRequest json) {
        Goodscategory goodscategory =  goodsService.getGoodsCategoryDetail(json.getId());
        GoodsCategoryDetailResponse goodsCategoryDetailResponse = new GoodsCategoryDetailResponse();
        goodsCategoryDetailResponse.setCode(0);
        goodsCategoryDetailResponse.setData(goodscategory);
        return goodsCategoryDetailResponse;
    }

    @Operation(summary = "获取商品分类商品列表")
    @PostMapping("/wxcloud/goods")
    GoodsResponse goods(HttpServletRequest request, @RequestBody GoodsRequest json) {
        Integer categoryId = json.getCategoryId();
        Integer shopId = json.getShopIds();
        List<Goods> goodsList = goodsService.getGoodsByCategoryId(categoryId,shopId);
        GoodsResponse goodsResponse = new GoodsResponse();
        goodsResponse.setCode(0);
        goodsResponse.setData(goodsList);
        return goodsResponse;
    }



    @Operation(summary = "获取商品详情")
    @PostMapping("/wxcloud/goodsDetail")
    GoodsDetailResponse goodsDetail(HttpServletRequest request, @RequestBody GoodsDetailRequest json) {
        GoodsDetailResponse goodsDetailResponse = new GoodsDetailResponse();
        String goodsId = json.getGoodsId();
        Goods goods = goodsService.getById(goodsId);
        GoodsDetail goodsDetail = new GoodsDetail();
        if (goods != null) {
            goodsDetail.setBasicInfo(goods);
            Integer categoryid = goods.getCategoryid();
            Goodscategory goodscategory = goodsService.getByGoodscategoryId(categoryid);
            goodsDetail.setCategory(goodscategory);
            List<PropertiesVo> properties = goodsService.getProperties(goodsId);
            goodsDetail.setProperties(properties);
            goodsDetail.setContent(goods.getContent());
            List<Pic>  pics = picService.getGoodsPics(goodsId);
            goodsDetail.setPics(pics);
            goodsDetailResponse.setData(goodsDetail);
            goodsDetailResponse.setCode(0);
            goodsDetailResponse.setMsg("success");
            return goodsDetailResponse;
        }
        goodsDetailResponse.setCode(-1);
        goodsDetailResponse.setMsg("fail");
        return goodsDetailResponse;
    }



    /**
     * 获取商品价格
     */
    @Operation(summary = "获取商品价格")
    @PostMapping("/wxcloud/goodsPrice")
    GoodsPriceResponse goodsPrice(HttpServletRequest request, @RequestBody GoodsPriceRequest json) {
        Integer goodsId = json.getGoodsId();
        String propertyChildIds = json.getPropertyChildIds();
        GoodsPrice price = skulistMapper.getGoodsPrice(goodsId, propertyChildIds);
        GoodsPriceResponse response = new GoodsPriceResponse();
        response.setData(price);
        response.setMsg("success");
        response.setCode(0);
        return response;
    }


    /**
     * 读取商品的可选配件
     */
    @Operation(summary = "读取商品的可选配件")
    @PostMapping("/wxcloud/goodsAddition")
    GoodsAdditionResponse goodsAddition(HttpServletRequest request, @RequestBody GoodsAdditionRequest json) {
        String goodsId = json.getGoodsId();
        List<Addition> goodsAdditions = goodsadditionService.getGoodsAddition(goodsId);
        List<Addition> additions = listTotree(goodsAdditions);
        GoodsAdditionResponse response = new GoodsAdditionResponse();
        response.setData(additions);
        response.setMsg("success");
        response.setCode(0);
        return response;
    }

    public static List<Addition> listTotree(List<Addition> platformList ){
        System.out.println(platformList.size());
        List<Addition> result = new ArrayList();
        for (Addition platform : platformList) {
            //获取根节点
            if(platform.getPid().equals(0)){
                result.add(platform);
            }
            for(Addition child : platformList){
                System.out.println(child.getPid().equals(platform.getId()));
                if(child.getPid().equals(platform.getId())){
                    platform.addChild(child);
                }
            }
        }
        return result;
    }

}
